//
//  Xm_HotelRollView.m
//  07-个人详情页(导航控制器注意点)
//
//  Created by xhkj on 2018/6/28.
//  Copyright © 2018年 小码哥. All rights reserved.
//


// 屏幕宽高
#define kk_WIDTH ([UIScreen  mainScreen].bounds.size.width)
#define kk_HEIGHT ([UIScreen mainScreen].bounds.size.height)
//RGB颜色
#define kRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

#import "Xm_HotelRollView.h"
#import "Masonry.h"
@interface Xm_HotelRollView ()<UIScrollViewDelegate>
{
    float indexTag;
    NSInteger tagbtn;
    
    
    float indexMinTag;
    NSInteger tagMinbtn;
    
    NSArray *titles;
    
    
    CGFloat  scorllOffsetX; // 记录scrllView 的偏移量
}
/** 用来存放所有子控制器view的scrollView */
@property (nonatomic, weak) UIScrollView *scrollView;

/** 上一次点击的标题按钮 */
@property (nonatomic, weak) UIButton *previousClickedTitleButton;


@property (nonatomic, weak) UIButton *titleButton;
@end


@implementation Xm_HotelRollView

 
-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self=[super initWithFrame:frame]) {
        
        indexTag=0;
        indexMinTag=0;
        
        
        UIView *rollView = [[UIView alloc] init];
        rollView.backgroundColor=[UIColor redColor];
        rollView.userInteractionEnabled=YES;
        [self addSubview:rollView];
        [rollView mas_makeConstraints:^(MASConstraintMaker *make) {


            make.top.mas_equalTo(10);
            make.right.mas_equalTo(-10);
            make.left.mas_equalTo(10);
            make.height.mas_offset(30);
            make.width.mas_offset(kk_WIDTH);
        }];

        UILabel *left_Lable = [[UILabel alloc] init];
        left_Lable.text=@"一间房一种生活";
        left_Lable.textAlignment=NSTextAlignmentCenter;
        [rollView addSubview:left_Lable];
        [left_Lable sizeToFit];
        [left_Lable mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.height.mas_offset(30);
        }];
        
        
        UIView *topView = [[UIView alloc] init];
        topView.backgroundColor=[UIColor greenColor];
        [rollView addSubview:topView];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_offset(30);
            make.width.mas_offset(100);
        }];
        
        UILabel *centerLable = [[UILabel alloc] init];
        centerLable.text=@"5";
        centerLable.textAlignment=NSTextAlignmentCenter;
        [topView addSubview:centerLable];
        [centerLable mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(topView.mas_centerX);
            make.centerY.mas_equalTo(topView.mas_centerY);
            make.height.mas_offset(30);
            make.width.mas_offset(30);
        }];
        
    
        UIButton *leftBtn = [[UIButton alloc] init];
        leftBtn.backgroundColor=[UIColor orangeColor];
        [leftBtn setImage:[UIImage imageNamed:@"leftBtn"] forState:UIControlStateNormal];
        [leftBtn addTarget:self action:@selector(titleButtonLeftClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.height.mas_offset(30);
            make.width.mas_offset(30);
        }];
        
        UIButton *rightBtn = [[UIButton alloc] init];
        rightBtn.backgroundColor=[UIColor orangeColor];
        [rightBtn setImage:[UIImage imageNamed:@"rightBtn"] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:rightBtn];
        
        [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_offset(30);
            make.width.mas_offset(30);
        }];
        
        
        
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.backgroundColor = [UIColor cyanColor];
        scrollView.frame = CGRectMake(10,CGRectGetMaxY(topView.frame)+45, [UIScreen mainScreen].bounds.size.width-20, 90);
        scrollView.delegate = self;
        //    scrollView.contentInset=UIEdgeInsetsMake(25, 0, 49, 0);
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        [self addSubview:scrollView];
        self.scrollView = scrollView;
     
        
        titles = @[@"  1  ", @" 2 ",@"3  ",@" 4 ",@"5 ",@"6  ",@" 7 ",@" 8 "];
        //titles = @[@"  1  ", @" 2 ",@"3  "];
        
        // 标题按钮的尺寸
        CGFloat titleButtonW = ([UIScreen mainScreen].bounds.size.width-40) / 3;//titles.count;
        CGFloat titleButtonH = 90;
        
        // 创建5个标题按钮
        for (NSUInteger i = 0; i < titles.count; i++) {
            UIButton *titleButton = [[UIButton alloc] init];
            titleButton.backgroundColor=kRGBColor(111, 222, 222);
            titleButton.tag = i;
            //        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [scrollView addSubview:titleButton];
            self.titleButton=titleButton;
            // frame
            titleButton.frame = CGRectMake(i * (titleButtonW+10),0, titleButtonW, titleButtonH);
            // 文字
            [titleButton setTitle:titles[i] forState:UIControlStateNormal];
        }
        
        scrollView.contentSize=CGSizeMake(titles.count*(titleButtonW+10), 0);
        
        //self.frame=scrollView.frame;
    }
    
    return self;
}


#pragma mark - 监听
/**
 *  点击标题按钮  3  0
 4  1
 5  2
 6  3
 7  4
 */
- (void)titleButtonClick:(UIButton *)titleButton
{
    tagbtn+=1;
    indexTag+=0.3333;
    
    //minux
    tagMinbtn=0;
    
    indexMinTag=0;
    
    if (titles.count==3) {
        return;
    }
    
    // 切换按钮状态
    [UIView animateWithDuration:0.25 animations:^{
        // 滚动scrollView
        CGFloat offsetX = (self.scrollView.frame.size.width-20) * self->indexTag+ self->tagbtn*10;
        
        CGFloat  titleMaxx=CGRectGetMaxX(self.titleButton.frame)-3*([UIScreen mainScreen].bounds.size.width-20) / 3;
        if (offsetX>=titleMaxx) {
            
            return ;
        }
        
        self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y);
        
        self->scorllOffsetX=offsetX;
        
        
//        CGFloat  titleMinx=CGRectGetMinX(self.titleButton.frame);
//        CGFloat  titleMaxx=CGRectGetMaxX(self.titleButton.frame);
//        self.titleButton.frame = CGRectMake(-self->tagbtn*titleMinx,0,([UIScreen mainScreen].bounds.size.width-20) / 3, 90);
        
    } completion:^(BOOL finished) {
        // 添加子控制器的view
        // [self addChildVcViewIntoScrollView:index];
    }];
}


- (void)titleButtonLeftClick:(UIButton *)titleButton
{
    tagMinbtn+=1;

    indexMinTag+=0.3333;
    
    // addroolView
    tagbtn=0;
    
    indexTag=0;
    
    
    if (titles.count==3) {
        return;
    }
    
    // 切换按钮状态
    [UIView animateWithDuration:0.25 animations:^{
        // 滚动scrollView
        CGFloat offsetX = self->scorllOffsetX -  (self.scrollView.frame.size.width-20) * self->indexMinTag- self->tagMinbtn*10;
        
        if (offsetX<=0) {
            offsetX=0;
        }
        
        self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y);
        
    } completion:^(BOOL finished) {
        // 添加子控制器的view
        // [self addChildVcViewIntoScrollView:index];
    }];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    NSLog(@"===index  :%f",scrollView.contentOffset.x);
    
    
    NSLog(@"===index  :%lu",(unsigned long)index);
    
}

@end
